package com.thb.pay.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.payment.PayInterfaceTypeServiceApi;
import com.thb.pay.api.payment.vo.req.PayInterfaceTypePageReq;
import com.thb.pay.api.payment.vo.req.PayInterfaceTypeReq;
import com.thb.pay.api.payment.vo.resp.PayInterfaceTypeVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.entity.SuperEntity;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.PayInterfaceType;
import com.thb.pay.payment.service.PayInterfaceTypeService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 支付-接口-类型表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class PayInterfaceTypeController implements PayInterfaceTypeServiceApi {

    @Autowired
    private PayInterfaceTypeService payInterfaceTypeService;

    @Override
    public PageResult<PayInterfaceTypeVo> findPage(@RequestBody PayInterfaceTypePageReq pageReq) {
        IPage<PayInterfaceType> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<PayInterfaceType> pageResult = payInterfaceTypeService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, PayInterfaceTypeVo.class);
    }

    @Override
    public List<PayInterfaceTypeVo> findAllList() {
        List<PayInterfaceTypeVo> resultDataList = Lists.newArrayList();
        List<PayInterfaceType> targetDbResultList = payInterfaceTypeService.selectList(null);
        targetDbResultList.forEach(dbResult -> {
            PayInterfaceTypeVo resultData = new PayInterfaceTypeVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public List<PayInterfaceTypeVo> findListByIds(@RequestBody List<Long> ids) {
        List<PayInterfaceTypeVo> resultDataList = Lists.newArrayList();
        LambdaQueryWrapper<PayInterfaceType> wrapper = new LambdaQueryWrapper();
        wrapper.in(PayInterfaceType::getId, ids);
        List<PayInterfaceType> targetDbResultList = payInterfaceTypeService.selectList(wrapper);
        targetDbResultList.forEach(dbResult -> {
            PayInterfaceTypeVo resultData = new PayInterfaceTypeVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public void add(@RequestBody PayInterfaceTypeReq payInterfaceTypeReq) {
        PayInterfaceTypeVo payInterfaceTypeVo = getByCode(payInterfaceTypeReq.getCode());
        if (payInterfaceTypeVo != null) {
            throw RRException.build("payInterfaceType.code.exist");
        }
        PayInterfaceType payInterfaceType = new PayInterfaceType();
        BeanUtil.copyProperties(payInterfaceTypeReq, payInterfaceType);
        payInterfaceTypeService.insert(payInterfaceType);
    }

    @Override
    public void update(@RequestBody PayInterfaceTypeReq payInterfaceTypeReq) {
        PayInterfaceType payInterface = payInterfaceTypeService.getById(payInterfaceTypeReq.getId());
        if (payInterface == null) {
            throw RRException.build("payInterfaceType.not.exist");
        }
        if (!payInterface.getCode().equals(payInterfaceTypeReq.getCode())) {
            PayInterfaceTypeVo payInterfaceTypeVo = getByCode(payInterfaceTypeReq.getCode());
            if (payInterfaceTypeVo != null) {
                throw RRException.build("payInterfaceType.code.exist");
            }
        }
        PayInterfaceType targetPayInterfaceType = new PayInterfaceType();
        BeanUtil.copyProperties(payInterfaceTypeReq, targetPayInterfaceType);
        payInterfaceTypeService.updateById(targetPayInterfaceType);
    }

    @Override
    public PayInterfaceTypeVo getByCode(@RequestParam("code") String code) {
        LambdaQueryWrapper<PayInterfaceType> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayInterfaceType::getCode, code);
        List<PayInterfaceType> payInterfaceTypeList = payInterfaceTypeService.selectList(wrapper);
        if (CollUtil.isNotEmpty(payInterfaceTypeList)) {
            PayInterfaceType payInterfaceType = payInterfaceTypeList.get(0);
            PayInterfaceTypeVo resultData = new PayInterfaceTypeVo();
            BeanUtil.copyProperties(payInterfaceType, resultData);
            return resultData;
        }
        return null;
    }

    @Override
    public PayInterfaceTypeVo getById(@RequestParam("id") Long id) {
        PayInterfaceType payInterfaceType = payInterfaceTypeService.selectById(id);
        if (payInterfaceType != null) {
            PayInterfaceTypeVo resultData = new PayInterfaceTypeVo();
            BeanUtil.copyProperties(payInterfaceType, resultData);
            return resultData;
        }
        return null;
    }

    @Override
    public void deleteBatchIds(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<PayInterfaceType> wrapper = new LambdaQueryWrapper();
        wrapper.in(PayInterfaceType::getId, ids);
        List<PayInterfaceType> targetDbResultList = payInterfaceTypeService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            targetDbResultList.forEach(payType -> {
                payType.setDelFlag(SuperEntity.DEFAULT_DELETE_FLAG);
            });
            payInterfaceTypeService.updateBatchById(targetDbResultList);
        }
    }
}
